package com.taptech.doufu.util.at;

import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.taptech.doufu.ui.activity.NewOthersPersonalActivity;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.services.personalcenter.AccountService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class AtUtil
{

    private int type;

    public AtUtil(TextView textView, String contentString, int type)
    {
        this.type = type;
        setAtContent(textView, contentString);

    }

    public void setAtContent(TextView textView, String contentString)
    {
        // TODO Auto-generated method stub
        textView.setText(getClickableSpan(contentString));
        textView.setMovementMethod(LinkMovementMethod.getInstance());
        if (type == Constant.AT_WRITE)
        {
            ((EditText) textView).setSelection(contentString.length());
        }

    }

    private SpannableString getClickableSpan(String contentString)
    {

        SpannableString spanableInfo = new SpannableString(contentString);
        Pattern p = Pattern.compile("(@[^@\\s#]{2,})");
        Matcher m = p.matcher(contentString);
        while (m.find())
        {
            int start = m.start();
            int end = m.end();
            spanableInfo.setSpan(new Clickable(contentString.substring(start + 1, end)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return spanableInfo;
    }

    public class Clickable extends ClickableSpan implements OnClickListener
    {

        private String user_nickName;

        public Clickable(String user_nickName)
        {
            // TODO Auto-generated constructor stub
            this.user_nickName = user_nickName;

        }

        @Override
        public void updateDrawState(TextPaint ds)
        {
            ds.setColor(Color.parseColor("#ff6e70"));
            ds.setUnderlineText(false);
        }

        @Override
        public void onClick(final View v)
        {
            if (type == Constant.AT_CLICK)
            {
                if (AccountService.getInstance().isLogin())
                {
                    if (AccountService.getInstance().getBaseAccount() != null && !AccountService.getInstance().getBaseAccount().getNickname().trim().equals(user_nickName))
                    {
                        Intent intent = new Intent(v.getContext(), NewOthersPersonalActivity.class);
                        intent.putExtra(Constant.USERNICKNAME, user_nickName);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        v.getContext().startActivity(intent);
                    }
                } else
                {
                    Intent intent = new Intent(v.getContext(), NewOthersPersonalActivity.class);
                    intent.putExtra(Constant.USERNICKNAME, user_nickName);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    v.getContext().startActivity(intent);
                }
            }

        }
    }

    public static String getTime()
    {
        // TODO Auto-generated method stub
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss：SSS");// 设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间

    }
}
